public without sharing class DatauploadController{
	public Blob body {get;set;}//内容;
    public List<MatchBean> fields{get;private set;}
    
    public List<SelectOption> fopt {get;private set;}///对象选择
    public List<SelectOption> fieldsOpt {get;private set;}//字段选择
    public String selectedObj {get;set;}//选中的对象
    public List<String> selectFields {get;set;}//选中的字段
    public Map<String,SObjectType> objMap;
    public String rs {get;set;}
    public Integer step {set;get;}//步骤
    public String separator {get;set;} //字段分割符号
    public void init(){
        step = 1;
        fopt = new List<SelectOption>();
        selectFields = new List<String>();
        objMap = new Map<String,SObjectType>();
        separator =  ',';

        ///获取所有对象
		for(Schema.sobjecttype s: Schema.getGlobalDescribe().values()){
                //选择自定义对象//&& s.getDescribe().isSearchable() == true
                if(s.getDescribe().IsCustom() == true || s.getDescribe().IsCustom() == false) {
                    //保存
                    objMap.put(s.getDescribe().getName(),s);
                	fopt.add(new SelectOption(s.getDescribe().getName(),s.getDescribe().getLabel()));
                }
		}
    }
   //获取选取对象的字段
    public void getFields(){
       if(selectedObj != null && selectedObj.length()>0){
       		 if(objMap.containsKey(selectedObj)){
       		 	SObjectType s = objMap.get(selectedObj);
            //获取字段
            	fieldsOpt = new List<SelectOption>();
            	for(Schema.SObjectField f :s.getDescribe().fields.getMap().values()){
            		fieldsOpt.add(new SelectOption(f+'',f+''));
            	}
        	}
        step = 2;
       }else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'请选择对象!'));
       }

    }
    
    public PageReference upload(){
        List<String> fs = new List<String>();
        
        for(MatchBean m :fields){
        	if(m.fieldVal != null)fs.add(m.fieldVal);
        }
        
        SObjectType s = objMap.get(selectedObj);
		if(fs.size() == fields.size()){
        	 ReadAndPopulateBatch  b = new ReadAndPopulateBatch(rs,s,fs,separator);
       		 database.executeBatch(b);
        
       		 return new PageReference('/apexpages/setup/listAsyncApexJobs.apexp?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DJobs&setupid=AsyncApexJobs');
        }else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'请完成所有字段匹配!'));
            return null;
        }
        
    }
    public void test (){
        System.debug('===>test');
    }
    ///
    public void next(){
        if(body == null){
        	   ApexPages.addMessage(
            		new ApexPages.Message(ApexPages.Severity.ERROR,'请上传数据!'));
        }else{
            try{
            	rs  = body.toString();
            	if(rs != null && rs.length() > 0){
        			fields  = new List<MatchBean>();
                    //判断换行符 默认为\n 
       				String line_s = '\n';
       				if(rs.contains('\r\n')) {
       					line_s = '\r\n';
       				}else{
       					if(rs.contains('\r'))line_s = '\r';
       				}
                    
        			for(String s :rs.substring(0,rs.indexOf(line_s)).split(separator,-1)){
        				MatchBean m = new MatchBean();
            			m.fieldKey = s;
            			fields.add(m);
        			}
       				rs = rs.substring(rs.indexOf(line_s)+1,rs.length());
       // System.debug(rs);r
       
  				step = 3;
                }else{
        	 		ApexPages.addMessage(
            			new ApexPages.Message(ApexPages.Severity.ERROR,'请上传数据!'));
        	
        		}    
            }catch(exception e){
            	ApexPages.addMessage(
            			new ApexPages.Message(ApexPages.Severity.ERROR,'字段分隔符错误:'+e.getMessage()));
            }
			
        }
    }
    ///中间类
    public class MatchBean{
    	public String fieldKey {get;set;}// 数据的字段名
        public String fieldVal {get;set;}// 匹配的字段名
    }
        
        
}